package com.qfedu.service.impl;

import com.qfedu.mapper.BranchMapper;
import com.qfedu.model.Branch;
import com.qfedu.page.PageResult;
import com.qfedu.page.QueryPageBean;
import com.qfedu.service.IBranchService;
import com.qfedu.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 概述:
 *
 * @Author: Tbh
 * @Date: 2022/7/1 14:15
 */

@Service
public class BranchServiceImpl implements IBranchService {

    @Autowired
    private BranchMapper branchMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPage) {

        Integer pageSize = queryPage.getPageSize();
        Integer currentPage = queryPage.getCurrentPage();

        List<Branch> findPage = branchMapper.findPage((currentPage - 1) * pageSize,pageSize);

        Long count = branchMapper.count();

        return new PageResult(count,findPage);
    }

    @Override
    @Transactional
    public int addBranch(Branch branch) {
        branch.setBid(UUIDUtils.uuid());
        branch.setUpdatetime(new Date());
        int row = branchMapper.addBranch(branch);
        return row;
    }

    @Override
    public Branch findByBid(String bid) {
        Branch branch = branchMapper.findByBid(bid);
        return branch;
    }

    @Override
    @Transactional
    public int edit(Branch branch) {
        int row = branchMapper.updateBranch(branch);
        return row;
    }

    @Override
    @Transactional
    public int deleteByBid(String bid) {
        int row = branchMapper.deleteByBid(bid);
        return row;
    }

    @Override
    public List<Branch> findAll() {
        return branchMapper.findAll();
    }
}
